package com.enjoyor.soa.traffic.core.tsms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.tsms.domain.ExecuteTaskDispatch;
import com.enjoyor.soa.traffic.core.tsms.mapper.IExecuteTaskDispatchMapper;
import com.enjoyor.soa.traffic.core.tsms.service.IExecuteTaskDispatchService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class ExecuteTaskDispatchService implements IExecuteTaskDispatchService{

	@Autowired
	private IExecuteTaskDispatchMapper iExecuteTaskDispatchMapper;
	
	@Autowired
	private IPubLogMapper iPubLogMapper;
	
	@Override
	public Page getExecuteTaskDispatchPage(Page page,String dispatchId, String executeTaskId) {
		// TODO Auto-generated method stub
		Map<String, Object> mapBean = new HashMap<>();
		mapBean.put("dispatchId",dispatchId);
		mapBean.put("executeTaskId",executeTaskId);
		page.setMapBean(mapBean);
		return iExecuteTaskDispatchMapper.getExecuteTaskDispatchPage(page);
	}

	@Override
	public List<ExecuteTaskDispatch> getExecuteTaskDispatchList(String dispatchId, String executeTaskId, String postId,
			String policeId, String policeName) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("dispatchId", dispatchId);
		map.put("executeTaskId", executeTaskId);
		map.put("postId", postId);
		map.put("policeId", policeId);
		map.put("policeName", policeName);
		return iExecuteTaskDispatchMapper.getExecuteTaskDispatchList(map);
	}
	
	@Override
	public void addExecuteTaskDispatch(ExecuteTaskDispatch executeTaskDispatch, String personName) {
		// TODO Auto-generated method stub
		String id = "PD"+ TimeHelper.getUniqueString();
		executeTaskDispatch.setDispatchId(id);
		iExecuteTaskDispatchMapper.insertSelective(executeTaskDispatch);
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                String.format("添加派警信息:{%s} 操作成功!", new Object[] { executeTaskDispatch.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_EXECUTETASKDISPATCH_MANAGEMENT.getMessage());
	}

	@Override
	public void editExecuteTaskDispatch(ExecuteTaskDispatch executeTaskDispatch, String personName) {
		// TODO Auto-generated method stub
		iExecuteTaskDispatchMapper.updateById(executeTaskDispatch);
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("修改派警信息:{%s} 操作成功!", new Object[] { executeTaskDispatch.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_EXECUTETASKDISPATCH_MANAGEMENT.getMessage());
	}

	@Override
	public void deleteExecuteTaskDispatch(String dispatchId, String personName) {
		// TODO Auto-generated method stub
		iExecuteTaskDispatchMapper.deleteById(dispatchId);
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
                String.format("删除派警信息:{%s} 操作成功!", new Object[] {dispatchId}),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_EXECUTETASKDISPATCH_MANAGEMENT.getMessage());
	}

	@Override
	public String assignExecuteTaskDispatch(List<ExecuteTaskDispatch> list, String executeTaskId,
			String personName) {
		// TODO Auto-generated method stub
		String result = "";
		String isExistName = "";
		int isExistNum = 0;
		//删除旧执行任务的警员；
		//iExecuteTaskDispatchMapper.delExecuteTaskDispatchByExecuteTaskId(executeTaskId);
		//该任务关联新警员；
		for(ExecuteTaskDispatch executeTaskDispatch : list){
			String postId = executeTaskDispatch.getPostId();
			String policeId = executeTaskDispatch.getPoliceId();
			String policeName = executeTaskDispatch.getPoliceName();
			List<ExecuteTaskDispatch> seaList = getExecuteTaskDispatchList(null, executeTaskId, postId,
				 policeId, null);
			if(null != seaList && seaList.size() > 0) {
				isExistName += policeName + "、";
				isExistNum++;
			}else {
				executeTaskDispatch.setDispatchId("DP"+ TimeHelper.getUniqueString());
				executeTaskDispatch.setExecuteTaskId(executeTaskId);
				executeTaskDispatch.setDispatchPerson(personName);
				executeTaskDispatch.setDispatchTime(TimeHelper.dateToLongString());
				
				iExecuteTaskDispatchMapper.insertSelective(executeTaskDispatch);
			}
		}
		if("".equals(isExistName)) {
			result = list.size() + "名警员派警成功！";
		}else {
			result = list.size() - isExistNum + "名警员派警成功," + isExistName + "重复派警！";
		}
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                String.format("派警:{%s} 操作成功!", new Object[] {executeTaskId}),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_EXECUTETASKDISPATCH_MANAGEMENT.getMessage());
		return result;
	}

}
